package com.bibrApp.difu.actvity.chengjiao_mvp;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bibrApp.difu.actvity.others.IOtherView;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.fragment.jieru_mvp.JiedaiManagerBean;
import com.bibrApp.difu.utils.EncryptUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ChengjiaoPresenter extends BasePresenter<IChengjiaoView> {
    public ChengjiaoPresenter(Context context, IChengjiaoView iView) {
        super(context, iView);
    }


    public void getDatas(int pageNum, String coinType, String type) {
//        pageSize Integer 是 每页条数
//        pageNumber Integer 是 当前页码
//        type Integer 是 广告类型；0-借出，1-借入 2-全部
//        status Integer 是 0-上架、1-下架、2-暂停、3-待交割
//        cycle Integer 否 周期
//        pawnCoinId String 否 抵押币币种id
//        sort Integer 否 0-数量升序、1-数量降序
//        dailyRateSort Integer 否 0-日利率升序 1-日利率降序
//        incomeSort Integer 否 0-收益升序 1-收益降序
//        pawnRateSort Integer 否 0-抵押率升序 1-抵押率降序
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageSize", 10);
        map.put("pageNumber", pageNum);
        if(!TextUtils.isEmpty(coinType)){
            map.put("pawnCoinId", coinType);
        }
        if(!TextUtils.isEmpty(type)){
            map.put("type", type);
        }

        RetrofitNet.getInstance()
                .getApi()
                .getTradeList(EncryptUtils.encryptNewType(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        JSONObject jsonObject = JSON.parseObject(EncryptUtils.objectToJson(value));
                        List<ChengjiaoBean> list = JSON.parseArray(jsonObject.getString("list"), ChengjiaoBean.class);
                        iView.onDataResult(list, jsonObject.getBoolean("hasNextPage"));
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                        iView.onDataResult(new ArrayList<>(), false);
                    }
                });
    }
}
